import cf from "../../../utils/config2"
// import Request from "../../../utils/request.js";
import CHARTS from '../../../lib/js/wxcharts-min.js';
// import Big from '../../../lib/js/big.min.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:1,
    checked:true,
    countData:{},
    totalPage:0,//分页总数
    slideOffset: 0, //指示器每次移动的距离
    activeIndex: 0, //当前展示的Tab项索引
    sliderWidth: 96, //指示器的宽度,计算得到
    canvasWidth: 176,
    canvstyle:"width: 176px;height: 176px;",
    contentHeight: 0, //页面除去头部Tabbar后，内容区的总高度，计算
    dataInfo: [
        {
            id: 1,
            subNum: "C1609050001",
            percentage: 30,
            grade: "SPCC",
            spec: "2.5*1200*C",
            weight: 500
        },
        {
            id: 2,
            subNum: "A1609050001",
            percentage: 80,
            grade: "SPCC",
            spec: "3.5*1200*C",
            weight: 100
        }
    ],
    visitedQuery:{
      // online:0,
      currPage:1,
      pageSize:10
    },
    noVisitedQuery:{
      currPage:1,
      pageSize:10
    },
    noVisitList:[],
    visitedList:[],
    online:[{key:'',name:'是否线上化'},{key:0,name:'未线上化'},{key:1,name:'已线上化'}],
    onlineIdx:0,
    customerStatus:['全部状态','丢失','回款','签约','认筹','认购','跟进中'],
    customerStatusIdx:0,
    reportedChannel:['全部渠道','其他','去电','合作方','合作方分销','慧销售','智美分销','智美置家','金科神器','智美置家推荐','来电','潜客录入','自然来访'],
    channelIdx:0
},
pieShow: function(data) {
  let piewidth = this.data.canvasWidth
  let {countData} = this.data
    var _tempSeries = [
      {
        name: "未线上化客户",
        data:countData.notOnlineCount||'',
        color: '#7A9BFF'
      },
      {
        name: "线上化客户",
        data:countData.onlineCount||'',
        color: '#39CF6D'
      }
    ]
  let pie = {
      canvasId: 'pieGraph', // canvas-id
      type: 'ring', // 图表类型，可选值为pie, line, column, area, ring
      series: _tempSeries,
      width: piewidth-10, // 宽度，单位为px
      height: piewidth-10, // 高度，单位为px
      legend: false, // 是否显示图表下方各类别的标识
      dataLabel: false, // 是否在图表中显示数据内容值
      extra: {
          ringWidth: piewidth/4 - 10,//圆环的宽度
          pie: {
              offsetAngle: -45
          }
      }
  };
  new CHARTS(pie);
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // let {dataInfo}=this.data
    var that = this;
    // console.log(phoneNo)
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          //计算相关宽度
          sliderWidth: res.windowWidth ,
          sliderOffset: res.windowWidth  * that.data.activeIndex,
          contentHeight: res.windowHeight - res.windowWidth / 750 * 68-12 //计算内容区高度，rpx -> px计算
        });
      }
    });
    /*计算饼图在显示器宽度*/
    let bodyWidth = wx.getSystemInfoSync().windowWidth
    let canvasWidth = bodyWidth*0.55
    let canvstyle = "width: "+canvasWidth+"px;height: "+canvasWidth+"px;"
    /*计算饼图在显示器宽度*/
    this.setData({
      canvstyle,
      canvasWidth
    });
    
    this.getCustomerGroupCount();
    this.getNoVisit();
  },

 getCustomerGroupCount(){
  let phoneNo = wx.getStorageSync('phoneNo');   
  let token = wx.getStorageSync('authName');   
   var _this=this
   let {dataInfo}=_this.data
   wx.request({
     url: cf.dataCenter+`zmzj/data/getCustomerGroupCountByAdviserPhone/${phoneNo}`,
     data: {},
     method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
     header: {
       token:token
     }, // 设置请求的 header
     success: function(res){
       _this.setData({
        countData:res.data.data
       })
       _this.pieShow(dataInfo)
     },
     fail: function() {
       // fail
     },
     complete: function() {
       // complete
     }
   })
 },
 //切换初始化参数
 init(){
   this.setData({
    totalPage:0,
    onlineIdx:0,
    noVisitList:[],
    noVisitedQuery:{
      currPage:1,
      pageSize:10
    },
    visitedList:[],
    visitedQuery:{
      currPage:1,
      pageSize:10
    }
   })
 },
 toNoVisit(){
   this.init();
   this.getNoVisit();
 },
 getNoVisit(query,opt){
  let phoneNo = wx.getStorageSync('phoneNo');   
  let token = wx.getStorageSync('authName');   
  wx.showLoading({
    title:'正在获取...'
   })
  var _this=this
  let {noVisitedQuery,noVisitList}=_this.data
  let {checked}=_this.data
  query={...opt,...noVisitedQuery,
    adviserPhone:phoneNo,
  }
  wx.request({
    url: cf.dataCenter+`zmzj/data/queryCustomerNotVisitedData/${query.currPage}/${query.pageSize}`,
    data: query,
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      token
    }, // 设置请求的 header
    success: function(res){
      // success
      wx.hideLoading();
      let {list,totalPage}=res.data.data
      noVisitList=[...noVisitList,...list]
      _this.setData({
        noVisitList,
        type:1,
        totalPage
      })

    }
  })
 },
 toVisited(){
  this.init();   
  this.getVisited();
 },
 getVisited(query,opt){
  let phoneNo = wx.getStorageSync('phoneNo');   
  let token = wx.getStorageSync('authName');   
   wx.showLoading({
    title:'正在获取...'
   })
  var _this=this
  let {visitedQuery,visitedList}=_this.data
  // let {checked}=_this.data
  query={...opt,...visitedQuery,
    adviserPhone:phoneNo
  }
  wx.request({
    url: cf.dataCenter+`zmzj/data/queryCustomerVisitedData/${query.currPage}/${query.pageSize}`,
    data: query,
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      token
    }, // 设置请求的 header
    success: function(res){
      wx.hideLoading();
      // success
      let {list,totalPage}=res.data.data
      visitedList=[...visitedList,...list]
      _this.setData({
        visitedList,
        type:2,
        totalPage
      })
    }
  })
 },
 //是否线上化
 changeOTO(e){
   let {noVisitedQuery,visitedQuery,online,type}=this.data
   let index = e.detail.value
   if(type==1){
    noVisitedQuery.online=online[index].key
    noVisitedQuery.currPage=1
    this.setData({
      noVisitList:[],
      noVisitedQuery,
      onlineIdx:index
     })
    this.getNoVisit('',noVisitedQuery)
   }else if(type==2){
    visitedQuery.online=online[index].key
    visitedQuery.currPage=1
    this.setData({
      visitedList:[],
      visitedQuery,
      onlineIdx:index
     })
    this.getVisited('',visitedQuery)
   }
   
  //  console.log(e.detail.value);
 },
 //选择渠道
 changeChannel(e){
   let {noVisitedQuery,reportedChannel}=this.data
   let channelIdx = e.detail.value
 
  noVisitedQuery.reportedChannel=channelIdx==0?'':reportedChannel[channelIdx]
  this.setData({
    channelIdx,
    noVisitedQuery,
    noVisitList:[]
  })
  this.getNoVisit('',noVisitedQuery)
 },
 //选择客户状态
 changeCustomerStatus(e){
  let {visitedQuery,customerStatus}=this.data
  let customerStatusIdx = e.detail.value
  visitedQuery.customerStatus=customerStatusIdx==0?'':customerStatus[customerStatusIdx]
  this.setData({
    customerStatusIdx,
    visitedQuery,
    visitedList:[]
  })
  this.getVisited('',visitedQuery)
 },
 //拨打电话
 tel(e){
  // console.log(e.currentTarget.dataset.tel)
  let {tel}=e.currentTarget.dataset
  wx.makePhoneCall({
    phoneNumber: tel
  })
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this=this
    let {type,noVisitedQuery,visitedQuery,totalPage} = _this.data
    
    if(type==1){
      noVisitedQuery.currPage++
      console.log(noVisitedQuery.currPage,totalPage)
      if(noVisitedQuery.currPage>totalPage) return
      _this.getNoVisit('',noVisitedQuery)
    }else if(type==2){
      visitedQuery.currPage++
      if(visitedQuery.currPage>totalPage) return
      _this.getVisited('',visitedQuery);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})